package com.chazz.dao.ljh.impl;

import com.chazz.dao.ljh.OrderDao;
import com.chazz.entity.*;
import com.chazz.utils.SqlUtils;

import java.util.ArrayList;
import java.util.List;

public class OrderDaoImpl implements OrderDao {
    @Override
    public List<String> getAddress(int i) {
        String sql  = "select adressvalue from adress where uid = ?";
        List<Adress> list = SqlUtils.select(Adress.class,sql,i);

        List<String> list1 = new ArrayList<>();
        for (Adress temp: list) {
           String str = temp.getAdressvalue();
          list1.add(str);
    }

        return list1;
    }

    @Override
    public Orders getOid(int id) {
        String sql="select * from orders where state=0 and uid=?";
        Orders order =SqlUtils.selectOne(Orders.class,sql,id);
        return order;
    }

    @Override
    public List<Orderdetails> getGid(int oid) {
        String sql="select * from orderdetails where oid = ?";
        List<Orderdetails> orderdetails = SqlUtils.select(Orderdetails.class,sql,oid);
        return orderdetails;
    }

    @Override
    public boolean setOrderId(int orderId) {
        String sql="select * from orders where ";
        return false;
    }

    @Override
    public boolean changeCount(List<Integer> gid, List<Integer> count) {
        String sql ="update sku set stock =stock - ? where goodsid = ? ";
        for (int i = 0 ;i<gid.size();i++){
            int tempGid = gid.get(i);
            int tempCount=count.get(i);
            SqlUtils.update(sql,tempCount,tempGid);
        }
        return true;
    }

    @Override
    public boolean changeState(int out_trade_no) {
        String sql ="update orders set state = 1 where id = ?";
        return SqlUtils.update(sql,out_trade_no)>0;
    }

    @Override
    public List<Orders> getOids(int uid) {
        String sql="select * from orders where state=1 and uid=?";
        List<Orders> orders =SqlUtils.select(Orders.class,sql,uid);
        return orders;
    }

    @Override
    public boolean setOrder(String orderAdress, int out_trade_no) {
        String sql = "update orders set orderadress = ? where id = ?";
        return SqlUtils.update(sql,orderAdress,out_trade_no)>0;
    }


    @Override
    public List<Goods> getGoodMessage(List<Integer> gid) {
        String sql ="select * from goods where goodsid = ?";
        List<Goods> goods = new ArrayList<>();
        for (Integer temp:gid) {
           goods.add(SqlUtils.selectOne(Goods.class,sql,temp));
        }
        return goods;
    }

    @Override
    public List<Category> getCategory(List<Integer> categoryId) {
        String sql="select * from category where categoryid = ?";
        List<Category> categories = new ArrayList<>();
        for (Integer temp:categoryId) {
            categories.add(SqlUtils.selectOne(Category.class,sql,temp));
        }
        return categories;
    }

    @Override
    public List<Brand> getBrand(List<Integer> brandId) {
        String sql="select * from brand where brandid=?";
        List<Brand> brands = new ArrayList<>();
        for (Integer temp:brandId) {
            brands.add(SqlUtils.selectOne(Brand.class,sql,temp));
        }
        return brands;
    }

    @Override
    public List<Attributekey> getAttributekey(List<Integer> categoryId) {
        String sql="select * from attributekey where categoryid = ?";
        List<Attributekey> attributekeys = new ArrayList<>();
        for (Integer temp:categoryId) {
            attributekeys.add(SqlUtils.selectOne(Attributekey.class,sql,temp));
        }
        return attributekeys;
    }

    @Override
    public List<Value> getValue(List<Integer> attributeId) {
        String sql="select * from value where attributeid = ?";
        List<Value> values = new ArrayList<>();
        for (Integer temp:attributeId) {
            values.add(SqlUtils.selectOne(Value.class,sql,temp));
        }
        return values;
    }

    @Override
    public List<Sku> getSku(List<Integer> gid) {
        String sql="select * from sku where goodsid = ?";
        List<Sku> skus = new ArrayList<>();
        for (Integer temp:gid) {
            skus.add(SqlUtils.selectOne(Sku.class,sql,temp));
        }
        return skus;
    }
}


